package com.yejhxt.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.yejhxt.apiInterface.ProfileInterface;
import com.yejhxt.controllerImpl.ProfileController;
import com.yejhxt.inter.roleMapper;
import com.yejhxt.inter.userMapper;
import com.yejhxt.model.role;
import com.yejhxt.model.user;
import com.yejhxt.tools.ProjectTools;


@Controller
@RequestMapping(value="api/user/profile",method = RequestMethod.POST)
public class Profile implements ProfileInterface{
	@Resource
	private userMapper userMapper;
	@Resource
	private roleMapper roleMapper;
	
	boolean first;

	void prepare(HttpServletResponse response) {
		if (first) {
			uc = new ProfileController(roleMapper );
			first = false;
		}
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
	}

	ProfileController uc;

	public Profile() {
		first = true;
	}

	@RequestMapping(value = "getAlluser.do")
	public void getAlluser( HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) {
		
	 int status;
	 List<role> roles = null;
		if (request.getRemoteUser() == null)
			status = 403;

		else {
			roles= roleMapper.selectAll();
		   
			status = 200;
		}
     
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200&&roles!=null) {
			map.put("status", "success");
			map.put("code", 200);
			map.put("result", roles);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {
        
			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "no user");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	@RequestMapping("get.do")
	@Override
	public void ProfileSelectHandler(@RequestParam(value = "access_token", defaultValue = "") String sid,
			HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		prepare(response);
		if (session != null) {
			user user = (user) session.getAttribute("User");
			if (user == null)
				session = ProjectTools.getAppSession(sid);
			if(session!=null)
			user = (user) session.getAttribute("User");
		}
		role person=uc.ProfileSelectHandler(session);
		Map<String, Object> map = new HashMap<String, Object>();
		if(person == null){
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");		
		}
		else{
			map.put("status", "success");
			map.put("code", 200);
			map.put("result", person);			
		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
		
	}

	@RequestMapping("update.do")
	

	@Override
	public void ProfileUpdateHandler(String sid, String realname, String gender, String occupation, String company,
			String address, String phone, String email, String qq, HttpServletRequest request,
			HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		if (session != null) {
			user user = (user) session.getAttribute("User");
			if (user == null)
				session = ProjectTools.getAppSession(sid);
			//if(session!=null)
			//user = (user) session.getAttribute("User");
		}

		int status = uc.ProfileUpdateHandler(sid, realname, gender, occupation, company, address, phone, email, qq, request, response, model, session);

		Map<String, Object> map = ProjectTools.returnMap(status);
		
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);		
	}

}